<template>
  <el-row>
    <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="120px"
    >
      <el-form-item label="文章类型"  prop="articleType">
        <el-select v-model = "form.articleType">
          <el-option v-for="(item,index) in typeList" :key="index" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题"   prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章简介"   prop="intro">
        <el-input v-model="form.intro"></el-input>
      </el-form-item>
      <el-form-item label="是否作为轮播"   prop="carousel">
        <el-radio-group v-model="form.carousel">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="封面"   prop="coverImg">
        <el-input v-model="form.coverImg"></el-input>
      </el-form-item>
<!-- 文章内容-->
      <el-form-item  label-width="0px">
        <tinymce v-model="form.mainInfo"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
       <span >
    <el-button  @click="$emit('close')">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </div>

  </el-row>
</template>

<script>
  import tinymce from "_c/tinymce";
  import Api from '@/api/article'
  import TypeApi from '@/api/newsType'

  export default {
    props: {
      data: {default: ''},
      type: {default: 1}
    },
    components: {tinymce},
    data() {
      return {
        loadData: false,
        typeList: [],
        form: {
          carousel: '0'
        },
        rules: {
          articleType: [
            {
              required: true,
              message: '请选择文章类型',
              trigger: 'blur'
            }
          ],
          title: [
            {
              required: true,
              message: '请输入文章标题',
              trigger: 'blur'
            }
          ],
          intro: [
            {
              required: true,
              message: '请输入文章简介',
              trigger: 'blur'
            }
          ],

        }
      }
    },
    watch: {
      data() {
        if (this.data) {
          this.form = JSON.parse(JSON.stringify(this.data))
        } else {
          this.form = {}
        }
      }
    },
    methods: {
      getType() {
        TypeApi.list({page:1,pageSize: 999}).then(res => {
          this.typeList = res.data
        })
      },
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.type ==1) {
              Api.add(this.form).then(() =>{
                this.$message.success('保存成功')
                this.$emit('success')
              })
            } else {
              Api.update(this.form).then(() =>{
                this.$message.success('保存成功')
                this.$emit('success')
              })
            }
          } else {
            return false
          }
        })
      }

    },
    created() {
      if (this.data) {
        this.form = JSON.parse(JSON.stringify(this.data))
      }
      this.getType()
    }
  }

</script>

<style scoped>

</style>
